<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <!-- 表单校验： -->
      <!-- el-form:  绑定 :model 和 :rules -->
      <!-- el-form-tem 设置 prop 属性 -->
      <el-form ref="form" :model="formData" :rules="rules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="formData.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="formData.password" type="password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="doLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  name: 'Login',
  data() {
    return {
      formData: {
        username: '',
        password: ''
      },
      rememberMe: false,
      // 校验规则
      rules: {
        username: [
          { required: true, message: '用户名不能为空' }
        ],
        password: [
          { required: true, message: '密码不能为空' },
          { min: 4, max: 10, message: '密码长度必须在 4 ~ 10 位之间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // TODO: 对存储进行抽离
    const userinfo = JSON.parse(localStorage.getItem('userinfo'))
    if (userinfo) {
      this.formData.username = userinfo.username
      this.formData.password = userinfo.password
      this.rememberMe = true
    }
  },
  methods: {
    doLogin() {
      // 手动校验（兜底）
      // this.$refs.form.validate(isOk => {
      //   console.log(isOk)
      // })
      this.$refs.form.validate()
        .then(() => {
          // console.log('校验成功')
          // TODO
          // 1. 调登录接口 ==> 拿token ==> 持久化
          return this.$store.dispatch('user/userLoginAction', this.formData)
        })
        .then(() => {
          this.rememberMe
            ? localStorage.setItem('userinfo', JSON.stringify(this.formData))
            : localStorage.removeItem('userinfo')
          // 2. 跳转到后台的首页 /workbench
          this.$router.push('/workbench')
        })
        .catch(() => {})
    }
  }
}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
